package com.roncoo.student.stu.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roncoo.student.stu.entity.User;
import com.roncoo.student.stu.dao.UserMapper;
import com.roncoo.student.stu.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.roncoo.ysq.config.CustomizeException;
import com.roncoo.ysq.config.MyCustomizeErrorCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jl
 * @since 2021-03-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;


    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.bucketName}")
    private String bucketName;

    //文件存储目录
    @Value("${aliyun.filehost}")
    private String filedir;


    @Override
    public String getPath(MultipartFile file) {
        String path="";
        //从文件名截取后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //获取随机码
        UUID uuid = UUID.randomUUID();
        //将随机码和后缀拼接，组成的新的文件名
        String objectName = uuid + suffix;
        try {
            //

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            path = "https://" + bucketName + "." + endpoint + "/" + objectName;
            //文件上传需要的参数  msg code data
            if (path!=null && path!=""){
                return path;
            }else {
                return null;
            }
        } catch (IOException e) {
            throw new CustomizeException(MyCustomizeErrorCode.IMAGE_UPLOAD_FAIL);
        }
    }

    @Override
    public Page findAll(User user, Page page) {

        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(user.getUname())){
            wrapper.like("uname",user.getUname());
        }
        if (StringUtils.isNotBlank(user.getUphone())){
            wrapper.like("uphone",user.getUphone());
        }
        if (user.getStatus()!=null){
            wrapper.eq("status",user.getStatus());
        }

        return userMapper.select(page,user,wrapper);

    }
}
